package mybatis.config;


import com.fasterxml.jackson.databind.ser.std.DateSerializer;
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
import org.springframework.boot.autoconfigure.jackson.Jackson2ObjectMapperBuilderCustomizer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author Leo
 * @Desc
 * @date 2023/6/14 16:12
 */
@Configuration
@ComponentScan
public class JacksonAutoConfiguration {
    private static final String DATETIME_FORMAT = "yyyy-MM-dd HH:mm:ss";

    /**
     */
    @Bean
    public Jackson2ObjectMapperBuilderCustomizer jackson2ObjectMapperBuilderCustomizer() {
        return builder -> {
            // Long 会自定转换成 String,分布式id 超过前端js解析的长度，会丢失精度，转换为string
            builder.serializerByType(Long.class, ToStringSerializer.instance);
            // 统一时间格式返回
            builder.serializerByType(Date.class, new DateSerializer(false, new SimpleDateFormat(DATETIME_FORMAT)));
        };
    }
}
